<?php

namespace App\Containers\AppSection\Qualification\Tasks;

use App\Containers\AppSection\Qualification\Data\Repositories\QualificationRepository;
use App\Ship\Criterias\Where;
use App\Ship\Parents\Tasks\Task;
use App\Ship\Traits\FilterByDateTrait;

class GetAllQualificationsTask extends Task
{
    use FilterByDateTrait;

    protected QualificationRepository $repository;

    public function __construct(QualificationRepository $repository)
    {
        $this->repository = $repository;
    }

    public function run()
    {
        return $this->repository->paginate();
    }

    public function withProfileStatus($status): self
    {
        $this->repository->whereHas('profile', function ($query) use ($status) {
            $query->where('qualification_status_key', $status);
        });

        return $this;
    }

    public function withStage($stage): self
    {
        $this->repository->pushCriteria(new Where('stage', '=', $stage));

        return $this;
    }

    public function forProfile($profileId): self
    {
        $this->repository->whereHas('profile', function ($query) use ($profileId) {
            $query->where('id', $profileId);
        });

        return $this;
    }

    public function active(): self
    {
        $this->repository->has('profiles.qualification');

        return $this;
    }
}
